<?php
session_start();
$id_user = $_SESSION["id_user"];
require_once "connexion.php";
require_once "Model/devisModel.php";
require_once "Model/clientModel.php";
require_once "Model/userModel.php";
//Appel au singleton Connexion
$maConnexion = ConnexionSingleton::getInstance();
$maConnexion->Connect();
$user = new userModel();
if($user->getStatut($id_user) == 1)
{	
?>
<div id="message_suppr" style="float:left;height:25px;width:100%;"> </div>
<div class="formulaire_devis">
<form name="form_devis" id="form_devis" action="" method="POST">
	<table class="tab_form_devis" width="100%">
		<tr class='fond_tr'>
			<td colspan='3' style='color:#404146;'> Ajout d'un devis</td>
			<td align="right"> <img src="css/images/bt_vert.png" id="ajouter"/></td>
		</tr>
		<tr>
			<td class="titre_champs" style="text-align:right;">Date : </td>
			<td class="titre_champs"> <input type="text" name="date_devis" id="date_devis" class="input_devis"/></td>
			<td class="titre_champs" style="text-align:right;">Client : </td>
			<td class="titre_champs"> 
				<select name="client_devis" id="client_devis">
					<option value=""> Selectionnez le client </option>
					<?php 
						$client = new clientModel();
						$tab_client = $client->getClientIdAndName();
						
						foreach($tab_client as $value)
						{
							echo '<option value='.$value["id_client"].'>'.$value["nom_client"].'</option>';
						}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<td class="titre_champs" style="text-align:right;">Montant : </td>
			<td class="titre_champs"> <input type="text" name="montant_devis" id="montant_devis" class="input_devis"/></td>
		</tr>
	</table>
	<input type="hidden" name="id_user" value="<?php echo $id_user;?>"/>
</form>	
</div>
<script>
	$('#ajouter').click(function()
	{
		var data = $('#form_devis').serialize();
		var clienDevis = $("#client_devis").val();
		var dateDevis = $("#date_devis").val();
		var montantDevis = $("#montant_devis").val();
		
		var hasError = false;

		if(clienDevis == "" || dateDevis == "" || montantDevis == "")
		{
			$('#message_suppr').html("Veuillez saisir tous les champs")
		}
		else
		{
			$.ajax(
			{
				   type: "POST",
				   url: "Controller/devisController.php?ajouter",
				   data: data,
				   cache: false,

				   success: function(msg)
				   { 
				   		$("#tableDevis tr:last").after(msg);
//						parent.fadeOut('slow', function() {$(this).remove();});
				   }
			 });		
		}
	});
	 $("#date_devis").datepicker($.datepicker.regional['fr']);
</script>
<?php 
}
?>
<div id="message" style="float:left;height:25px;width:100%;"> </div>
<div class="div_devis_devis" id="div_devis_devis">
	<?php 
			$devis = new devisModel();
			$tab_devis = $devis->getDevis(15);
			echo "<table id='tableDevis' class='tabDevis' width='100%'>";
			echo "<tr class='fond_tr'><td colspan='7' style='color:#404146;'> Liste des devis </td></tr>";
			echo "<tr style='color:#488BD2;background:#fff;'><td class='tab_td_style'> ID</td><td class='tab_td_style'> Montant </td><td class='tab_td_style'> Date</td><td class='tab_td_style'> Client</td><td class='tab_td_style'> Utilisateur </td><td class='tab_td_style' colspan='2'></td></tr>";
			foreach($tab_devis as $key => $value)
			{
				echo "<tr id='".$value['id']."' style='background:#fff;'>";
				echo "<td class='tab_td_style'>".$value["id"]."</td>";
				echo "<td class='tab_td_style'>".$value["montant"]."</td>";
				echo "<td class='tab_td_style'>".$value["date"]."</td>";
				echo "<td class='tab_td_style'>".$value["client"]."</td>";
				echo "<td class='tab_td_style'>".$value["utilisateur"]."</td>";
				if($user->getStatut($id_user) == 1)
				{		
					echo "<td class='tab_td_style'>";
					echo "<a class='delete'>";
					echo "<img src='css/images/supprimer.png' id='supression'>"; 
					echo "</a>";
					echo "</td>";
				}	
				echo "<td class='tab_td_style'>";
				echo "<a class='facture'>";
				echo "<img src='css/images/facture-icone.png' id='facture'>"; 
				echo "</a>";
				echo "</td>";
				echo "</tr>";	
			}
			
		?>
		<script>
			$('table#tableDevis td a.delete').live("click",function()
			{
				if (confirm("Etes vous sur de vouloir supprimer l'enregistrement ?"))
				{
					var id = $(this).parent().parent().attr('id');
					var data = 'id_devis='+id;
					var parent = $(this).parent().parent();

					$.ajax(
					{
						   type: "POST",
						   url: "Controller/devisController.php?suppression",
						   data: data,
						   cache: false,

						   success: function(msg)
						   {
								$('#message').html(msg);
								parent.fadeOut('slow', function() {$(this).remove();});
						   }
					 });
				}
			});
		</script>
		<script>
			$('table#tableDevis td a.facture').live("click",function()
			{
				if (confirm("Etes vous sur de vouloir definir ce devis en facture ?"))
				{
					var id = $(this).parent().parent().attr('id');
					var data = 'id_devis='+id;
					var parent = $(this).parent().parent();

					$.ajax(
					{
						   type: "POST",
						   url: "Controller/factureController.php?ajouter",
						   data: data,
						   cache: false,

						   success: function(msg)
						   {
								$('#message').html(msg);
//								parent.fadeOut('slow', function() {$(this).remove();});
						   }
					 });
				}
			});
		</script>
</div>